/*!
 * \file jxbankagree_message.c
 *
 * \brief JXBANKAGREE消息解析
 * 
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.
 *
 * \version 1.0
 * \author songbb
 * \date 2017/04/20
 *
 */

#include <iconv2utf8.h>
#include "conntrack.h"
#include "frct_comm.h"
#include "frreport_comm.h"

#include "jxbankagree_core.h"
#include "jxbankagree_comm.h"
#include "jxbankagree_dict.h"
#include "jxbankagree_message.h"

Frhash_header *p_hash_jxbankagree_trancode = NULL;

/*******************************************************************************
*  FUNC     :  释放 Jxbankagree_trancode
*  ARGS     :  **pp_st_trancode                             (Jxbankagree_trancode)
*  RTN      :  void
*  NOTE     :
*******************************************************************************/
FR_STATIC void __jxbankagree_free_trancode(Jxbankagree_trancode **pp_st_trancode)
{
    if (pp_st_trancode == NULL || *pp_st_trancode == NULL)
        return;

    jxbankagree_free((void **) &((*pp_st_trancode)->p_name));
    jxbankagree_free((void **) pp_st_trancode);

    return;
}

/*******************************************************************************
*  FUNC     :  释放 stmt node节点
*  ARGS     :  *p_st_node                               (Frlist_node)
*  RTN      :  void
*  NOTE     :
*******************************************************************************/
FR_STATIC void __jxbankagree_trancode_node_free(Frlist_node *p_st_node)
{
    if (p_st_node == NULL || p_st_node->data == NULL)
        return;

    Jxbankagree_trancode *p_st_sip_stmt = (Jxbankagree_trancode *) FRLIST_GET_NODE_DATA(p_st_node);
    __jxbankagree_free_trancode(&p_st_sip_stmt);
    p_st_node->data = NULL;
}

void jxbankagree_init_hash_trancode(void)
{
    int len = sizeof(arr_jxbankagree_trancode_maps) / sizeof(arr_jxbankagree_trancode_maps[0]);
    int i_loop = 0;
    Jxbankagree_trancode *p_st_trancode = NULL;
    Frlist_node *p_st_node = NULL;

    p_hash_jxbankagree_trancode = frhash2_create2(len);

    for (; i_loop < len; i_loop++)
    {
        if ((p_st_trancode = (Jxbankagree_trancode *) frmalloc(sizeof(Jxbankagree_trancode))) == NULL)
        {
            JXBANKAGREE_MALLOCK_ERR;
            return;
        }
        memset(p_st_trancode, 0x00, sizeof(Jxbankagree_trancode));

        p_st_trancode->p_name = frstrdup(arr_jxbankagree_trancode_maps[i_loop].arr_name);
        p_st_node = frlist_node_create(p_st_trancode);

        if (frhash2_addStr(p_hash_jxbankagree_trancode, arr_jxbankagree_trancode_maps[i_loop].arr_code,
                           (unsigned int) strlen(arr_jxbankagree_trancode_maps[i_loop].arr_code), p_st_node) != FR_SUCC)
        {
            __jxbankagree_free_trancode(&p_st_trancode);
            __jxbankagree_trancode_node_free(p_st_node);
            return;
        }
    }

    return;
}

void jxbankagree_destroy_hash_trancode(void)
{
    frhash2_free2(p_hash_jxbankagree_trancode, __jxbankagree_trancode_node_free);
    return;
}

int jxbankagree_req(Conntrack *p_st_conn
                    , L7session_jxbankagree *p_st_sess
                    , L7_jxbankagree_packet *p_st_jxbankagree_packet
                    , Frhash_header *p_st_hash_report)
{
    JXBANKAGREE_PRINT_INFO("jxbankagree_req...\n");
    Frreport_body_data *p_st_body = NULL;
    Jxbankagree_trancode *p_st_trancode = NULL;
    Frlist_node *p_st_node = NULL;
    char arr_hosttransactioncode[8+1] = {0};

    if (bytebuff_skip(p_st_jxbankagree_packet->p_st_packet, 18) != FR_SUCC)
        goto error;

    memset(arr_hosttransactioncode, 0x00, sizeof(arr_hosttransactioncode));
    if (bytebuff_get_bytes(p_st_jxbankagree_packet->p_st_packet, arr_hosttransactioncode, 8) != FR_SUCC)
        goto error;
    frtrim(arr_hosttransactioncode);
    JXBANKAGREE_PRINT_WARN("_HostTransactionCode:[%s]\n", arr_hosttransactioncode);

    if (arr_hosttransactioncode[0] != 0x00)
    {
        // 新建上报数据结构体
        if ((p_st_body = frreport_body_new_l7(p_st_conn)) == NULL)
            goto error;

        if (frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_TRANS".type_code", arr_hosttransactioncode) != FR_SUCC)
            goto error;

        p_st_node = frhash2_getStr(p_hash_jxbankagree_trancode, arr_hosttransactioncode, (unsigned int) strlen(arr_hosttransactioncode));

        if (p_st_node && (p_st_trancode = (Jxbankagree_trancode *) FRLIST_GET_NODE_DATA(p_st_node)) == NULL)
            goto error;
        if (p_st_trancode && p_st_trancode->p_name)
        {
            if (frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_TRANS_TITLE, p_st_trancode->p_name) != FR_SUCC)
                goto error;
            if (frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_TRANS".type_name", p_st_trancode->p_name) != FR_SUCC)
                goto error;
        }
        else
        {
            if (frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_TRANS_TITLE, arr_hosttransactioncode) != FR_SUCC)
                goto error;
            if (frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_TRANS".type_name", arr_hosttransactioncode) != FR_SUCC)
                goto error;
        }

        // 上报数据
        if (jxbankagree_report_msg_data_req(p_st_conn, p_st_jxbankagree_packet, p_st_body, p_st_hash_report) != FR_SUCC)
            goto error;
    }

    return FR_SUCC;
error:
    frreport_body_free(&p_st_body);
    return FR_FAIL;
}

int jxbankagree_resp(Conntrack *p_st_conn
                    , L7session_jxbankagree *p_st_sess
                    , L7_jxbankagree_packet *p_st_jxbankagree_packet
                    , Frhash_header *p_st_hash_report)
{
    JXBANKAGREE_PRINT_INFO("jxbankagree_resp...\n");
    Frreport_body_data *p_st_body = NULL;
    char arr_rejcode[7+1] = {0};
    char arr_rejmsg[192+1] = {0};
    char *p_rejmsg_utf8 = NULL;
    unsigned int ui_rejmsg_len = 0;

    if (bytebuff_skip(p_st_jxbankagree_packet->p_st_packet, 8) != FR_SUCC)
        goto error;

    memset(arr_rejcode, 0x00, sizeof(arr_rejcode));
    if (bytebuff_get_bytes(p_st_jxbankagree_packet->p_st_packet, arr_rejcode, 7) != FR_SUCC)
        goto error;
    JXBANKAGREE_PRINT_WARN("RejCode:[%s]\n", arr_rejcode);

    memset(arr_rejmsg, 0x00, sizeof(arr_rejmsg));
    if (bytebuff_get_bytes(p_st_jxbankagree_packet->p_st_packet, arr_rejmsg, 192) != FR_SUCC)
        goto error;
    if (iconv_to_utf8(arr_rejmsg, strlen(arr_rejmsg), "gb18030", &p_rejmsg_utf8, &ui_rejmsg_len) != FR_SUCC)
        goto error;
    frtrim(p_rejmsg_utf8);
    JXBANKAGREE_PRINT_WARN("RejMsg:[%s]\n", p_rejmsg_utf8);

    if (arr_rejcode[0] != 0x00)
    {
        // 新建上报数据结构体
        if ((p_st_body = frreport_body_new_l7(p_st_conn)) == NULL)
            goto error;
        if (frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_RESP_CODE_STRING, arr_rejcode) != FR_SUCC)
            goto error;

        if (p_rejmsg_utf8)
        {
            if (frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_RESP_ERR_MSG, p_rejmsg_utf8) != FR_SUCC)
                goto error;
        }

        // 上报数据
        if (jxbankagree_report_msg_data_resp(p_st_conn, p_st_jxbankagree_packet, p_st_body, p_st_hash_report) != FR_SUCC)
            goto error;
    }

    jxbankagree_free((void **) &p_rejmsg_utf8);
    return FR_SUCC;
error:
    jxbankagree_free((void **) &p_rejmsg_utf8);
    frreport_body_free(&p_st_body);
    return FR_FAIL;
}
